package com.caocaog.market.finance.pay;

import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.caocaog.market.constant.MarketConstant;
import com.caocaog.market.finance.pay.alipay.FinancePay;
import com.caocaog.market.finance.pay.vo.FinanceVo;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.exception.JeecgBootException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: UnionPay
 * @author: Yuki Guo
 * @date: 2021年11月01日 11:14
 */

@Slf4j
@Component
@EnableScheduling
public class FinanceUnionPay {

    @Autowired
    private FinancePay aliPay;

    public Map<String, Object> transfer(String orderNo, String zfbName, String zfbAccount,
                                        String amount, String remark, int type) {
        /* 生成转账记录 */
        FinanceVo transVo = new FinanceVo();
        transVo.setAmount(amount);
        String outBizNoStr;
        if (type == MarketConstant.FINANCE_BILL_LINE_TYPE_SHOP) {
            //商家
            outBizNoStr = "SHOP";
        } else if (type == MarketConstant.FINANCE_BILL_LINE_TYPE_SYS) {
            //总部
            outBizNoStr = "SYS";

        } else if (type == MarketConstant.FINANCE_BILL_LINE_TYPE_SITE) {
            //分站
            outBizNoStr = "SITE";
        } else {
            log.error("订单号:" + orderNo + "转账失败:type值错误~");
            throw new JeecgBootException("订单号:" + orderNo + "转账失败:type值错误~");
        }
        transVo.setOutBizNo(outBizNoStr + orderNo);
        transVo.setRemark(remark);
        transVo.setZfbAccount(zfbAccount);
        transVo.setZfbName(zfbName);

        AlipayFundTransUniTransferResponse response = aliPay. transfer(transVo);
        Map<String, Object> resultMap = new HashMap<>();
        String code = "";
        String msg = "FinancePay.transfer 未返回数据";
        boolean res = false;
        if (response != null) {
            if (response.isSuccess()) {
                res = true;
            }
            code = response.getCode() + "-" + response.getSubCode();
            msg = response.getMsg() + "-" + response.getSubMsg();
        }
        resultMap.put("isSuccess", res);
        resultMap.put("aliOrderNo", transVo.getOutBizNo());
        resultMap.put("code", code);
        resultMap.put("msg", msg);
        return resultMap;

    }

}

